package com.tyqx.hashchainbackend.controller;

import com.tyqx.hashchainbackend.service.TrackUserService;
import com.tyqx.hashchainbean.module.TrackUser;
import hashchainutil.common.*;
import hashchainutil.exception.HashchainException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/trackUser")
@CrossOrigin
public class TrackUserController {

    @Resource
    private TrackUserService trackUserService;


    //分页查询用户信息
    @RequestMapping(value = "/queryPage", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public BackendPage<List<TrackUser>> queryPage(int page, int limit, String userCode) {
        //构建参数
        Map<String, Object> param = new HashMap();
        if (EmptyUtils.isNotEmpty(userCode))
            param.put("userCode", userCode);
        try {
            BackendPage<List<TrackUser>> result = trackUserService.queryPageByMap(param, page, limit);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new BackendPage("查询异常");
        }
    }

    @RequestMapping(value = "/relogin", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public String relogin(TrackUser trackUser){
        String message="";
        try {
            Integer count=trackUserService.save(trackUser);
            if(count>0){
                message="success";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return message;
    }


    @RequestMapping(value = "/registerTrackUser", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Dto registerTrackUser(TrackUser trackUser){
        try {
            Integer count=trackUserService.save(trackUser);
            if(count>0){
                return DtoUtil.returnSuccess("添加成功");
            }
        } catch (HashchainException e) {
            e.printStackTrace();
            return DtoUtil.returnFail(e.getMessage(),e.getErrorCode());
        }
        return DtoUtil.returnFail("添加失败","10001");

    }
}
